package com.zbdx.controller;

import com.zbdx.domain.product;
import com.zbdx.service.ShoppingGoodsService;
import com.zbdx.service.impl.ShoppingGoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @version 1.0
 * @Auther 樊航宇
 * @Date 2024/9/16 16:00
 * @注释
 */
@WebServlet("/shopping/shoppingGoods")
public class shoppingGoodsServlet extends HttpServlet {

   private ShoppingGoodsService shoppingGoodsService = new ShoppingGoodsServiceImpl();

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


      //1.设置编码
      req.setCharacterEncoding("utf-8");
      resp.setContentType("text/html;charset=utf-8");
      //2.获取请求参数
      String pid = req.getParameter("pid");

      int id = Integer.parseInt(pid);
      System.out.println(pid);

      product product = null;
      try {
         product = shoppingGoodsService.selectById(id);
      } catch (SQLException e) {
         throw new RuntimeException(e);
      }
      req.setAttribute("product",product);

      System.out.println(product);

      req.getRequestDispatcher("shoppingGoods.jsp").forward(req,resp);

   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      this.doGet(req,resp);
   }
}
